﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace WXY.Course.Common.MVCExtension
{
    public static class HtmlExtension
    {
        /// <summary>
        /// 全角字符模式，即无论汉字还是英文都等于1个字符
        /// </summary>
        public static string CutStrInSBCCase(this HtmlHelper htmlHelper, string input, int length, string suffix = "")
        {
            return CutString2(input, length, suffix);
        }

        /// <summary>
        /// 半角字符模式，即一个汉字等于2个字符
        /// </summary>
        public static string CutStrInDBCCase(this HtmlHelper htmlHelper, string input, int length, string suffix = "")
        {
            return CutString(input, length, suffix);
        }

        /// <summary>
        ///  去除文本中的所有Html标签 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveHtmlTag(this HtmlHelper helper, string input)
        {
            if (input == null) input = string.Empty;
            string regexstr = @"(<[^>]*>)|(&.+?;)";
            return Regex.Replace(input, regexstr, string.Empty, RegexOptions.IgnoreCase);
        }

        public static string GetFirstParagraph(this HtmlHelper helper, string input)
        {
            if (input == null) input = string.Empty;
            string regexstr = @"(^<\s*p\s*>[\s\S]+?<\s*br\s*/\s*>)|(^<\s*p\s*>[\s\S]+?<\s*/\s*p\s*>)";
            return Regex.Match(input, regexstr).Value;
        }

        /// <summary>
        /// 将(headCount, Length-tailCount)范围内字符替换为*
        /// </summary>
        /// <param name="input">输入</param>
        /// <param name="headCount">头数量</param>
        /// <param name="tailCount">尾数量</param>
        /// <returns>替换后的内容</returns>
        public static MvcHtmlString Mask(this HtmlHelper helper, string input, int headCount, int tailCount)
        {
            if (string.IsNullOrEmpty(input))
            {
                return null;
            }

            char[] output = new char[input.Length];
            for (var i = 0; i < input.Length; i++)
            {
                if (i >= headCount && i < input.Length - tailCount)
                {
                    output[i] = '*';
                }
                else
                {
                    output[i] = input[i];
                }
            }
            return new MvcHtmlString(new string(output));
        }

        private static String CutString(String input, Int32 halfWidthCharacterCount, String suffix)
        {
            if (String.IsNullOrWhiteSpace(input)) return input;
            Int32 length = input.Length,
                cutIndex = 0,
                charCount = 0;
            while (cutIndex < length)
            {
                if (input[cutIndex] > 255) charCount += 2;
                else charCount++;
                if (charCount <= halfWidthCharacterCount) cutIndex++;
                else break;
            }
            if (cutIndex < length) return input.Substring(0, cutIndex) + suffix;
            else return input;
        }

        private static String CutString2(String input, Int32 characterCount, string suffix)
        {
            input = input != null ? input : String.Empty;
            if (input.Length > characterCount)
            {
                return input.Substring(0, characterCount) + suffix;
            }
            return input;
        }
    }
}